package com.example.drawing.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.drawing.R;

public class MyView extends View {
    Paint paint;


    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        Drawable MyReference = typedArray.getDrawable(R.styleable.MyView_MyReference);//引用资源id
        int color = typedArray.getColor(R.styleable.MyView_Mycolor, 0XFFFFFFFF);//颜色
        boolean MBoolean = typedArray.getBoolean(R.styleable.MyView_MyBoolean, false);//布尔值
        float dimension = typedArray.getDimension(R.styleable.MyView_Mydimension, 3600);//尺寸值
        float MyFloat = typedArray.getFloat(R.styleable.MyView_MyFloat, 3600);//浮点
        int integer = typedArray.getInteger(R.styleable.MyView_Myinteger, 3600);//整型值
        String string = typedArray.getString(R.styleable.MyView_Mystring);//字符串
        float fraction = typedArray.getFraction(R.styleable.MyView_Myfraction,1,1,0);//百分数,比如200%
        int Myflag = typedArray.getInteger(R.styleable.MyView_MYflag,0x00 );

        System.out.println(MyReference);
        System.out.println(color);
        System.out.println(MBoolean);
        System.out.println(dimension);
        System.out.println(MyFloat);
        System.out.println(integer);
        System.out.println(string);
        System.out.println(fraction);
        System.out.println(Myflag);
        typedArray.recycle(); // 用完要关闭回收资源，必须的强制性的

        init();

    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }


    //重写该方法，在这里绘图
    @Override
    protected void onDraw(Canvas canvas) {
//        drawClock(canvas);
        drawTriangle(canvas);
        drawRectangular(canvas);
        super.onDraw(canvas);
    }

    private void init() {
        paint = new Paint();   //初始化画笔
        paint.setColor(Color.RED);//画笔颜色
        paint.setAntiAlias(true);//画笔抗锯齿
        paint.setDither(true);//画笔防抖动。
        paint.setStyle(Paint.Style.STROKE);//画笔模式为填充
        paint.setStrokeJoin(Paint.Join.ROUND); //结合处为圆角
        paint.setStrokeCap(Paint.Cap.ROUND); // 设置转弯处为圆角
        paint.setStrokeWidth(30);   // 设置画笔宽度
    }

    /**
     * 绘制时钟
     */
    private void drawClock(Canvas canvas) {
        //圆心
        float y = 300;


        paint.setAntiAlias(true);//该方法作用是抗锯齿
        paint.setDither(true);//设置防抖动。
        paint.setFilterBitmap(true);//是用来对位图进行滤波处理。
        paint.setColor(getResources().getColor(R.color.teal_200));//画笔颜色
        paint.setStyle(Paint.Style.FILL);  //画笔风格
        paint.setTextSize(60);             //绘制文字大小，单位px
        paint.setStrokeWidth(5);           //画笔粗细

        canvas.translate(canvas.getWidth() / 2, canvas.getHeight() / 2); //将原点的位置移动画纸的中心
        canvas.drawCircle(0, 0, y, paint); //画圆圈

        //使用path绘制路径文字
        canvas.save();//保存当前的绘图状态。
        canvas.translate(0, -675);//移动画笔位置
        Path path = new Path();//设置路径
        //构造一个指定了4个参数的矩形 ，这些参数都是坐标
        RectF rectF = new RectF(0, 0, 250, 250);
        //画图函数: 外接矩形大小、起始角度 、跨越角度
        path.addArc(rectF, -180, 180);
        Paint citePaint = new Paint(paint);//在原画笔上新建画笔
        citePaint.setColor(getResources().getColor(R.color.black));//画笔颜色
        citePaint.setTextSize(50);//绘制文字大小
        citePaint.setStrokeWidth(50);//画笔粗细


        //hOffset : 与路径起始点的水平偏移距离 vOffset : 与路径中心的垂直偏移量
        canvas.drawTextOnPath("绘制表盘~", path, 0, 0, citePaint);
        canvas.restore();//恢复之前保存的绘图状态。


        Paint tmpPaint = new Paint(paint); //小刻度画笔对象
        tmpPaint.setStrokeWidth(1);//画笔粗细
        tmpPaint.setColor(getResources().getColor(R.color.black));//画笔颜色
        Paint tmpPaint2 = new Paint(paint); //小刻度画笔对象
        tmpPaint2.setStrokeWidth(10);//画笔粗细
        tmpPaint2.setColor(getResources().getColor(R.color.purple_200));//画笔颜色

        int count = 60; //总刻度数

        for (int i = 0; i < count; i++) {
            if (i % 5 == 0) {
                canvas.drawLine(0f, y, 0, y + 32f, tmpPaint2);//绘制直线
                canvas.drawText(String.valueOf(i / 5 + 1), -4f, y + 95f, tmpPaint);//绘制字

            } else {
                canvas.drawLine(0f, y, 0f, y + 20f, tmpPaint);//绘制直线
            }
            canvas.rotate(360 / count, 0f, 0f); //旋转坐标系 每次60度
        }

        //绘制指针
        tmpPaint.setColor(Color.RED);//设置画笔颜色
        tmpPaint.setStrokeWidth(10);//设置画笔粗细
        canvas.drawCircle(0, 0, 17, tmpPaint); //画圆圈
        tmpPaint.setStyle(Paint.Style.FILL);//描边并填充
        tmpPaint.setColor(Color.YELLOW);//设置颜色
        canvas.drawCircle(0, 0, 5, tmpPaint);//画圆圈
        canvas.drawLine(0, 10, 0, -265, tmpPaint);//绘画直线

        //绘制图片
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tp20220905100627);
//        canvas.drawBitmap(bitmap, 150, 150, paint);
    }

    /**
     * 绘制三角形
     */
    private void drawTriangle(Canvas canvas) {

        Path path = new Path();//设置路径
        path.moveTo(10, 10);
        path.lineTo(120, 230);
        path.lineTo(1310, 540);
        path.close();
        canvas.drawPath(path, paint);
    }

    /**
     * 绘制三角形
     */
    private void drawRectangular(Canvas canvas) {
        RectF rect = new RectF(900, 900, 300, 200);
        canvas.drawRoundRect(rect, 40, 40, paint);
    }
}